package codeRandomThoughts.Test513找树左下角的值;

import codeRandomThoughts.utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 层序遍历轻松拿下
 * 求最底层最左边的值
 */
public class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        int res = 0;
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            int first = size;

            while (size > 0) {
                root = queue.poll();
                if (size == first) {
                    res = root.val;
                }

                if (root.left != null) {
                    queue.offer(root.left);
                }
                if (root.right != null) {
                    queue.offer(root.right);
                }
                size--;
            }
        }
        return res;
    }
}
